<?php
/**
 * 文章设置
 */
namespace App\Http\Controllers;

use App\Models\Article;
use App\Models\Log;
use Illuminate\Http\Request;

class ArticleController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        return $this->show($request);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request,Article $article)
    {

        $data=$request->all();
        $article->title=$data['title'];
        $article->content=$data['content'];
        $article->read_num=$data['read_num'];
        $article->hot_bot=$data['hot_bot'];
        if($data['id']){
            $article->exists = true;
            $article->id = $data['id'];
        }
        try{
            if (!$article->save()) {
                Log::addLogs(trans('xxm.article.handle_article').trans('fzs.common.success'),'/article/story');
                return $this->resultJson(trans('fzs.common.fail'),0);
            }else{
                Log::addLogs(trans('xxm.article.handle_article').trans('fzs.common.fail'),'/article/store');
                return $this->resultJson(trans('fzs.common.success'),1);
            }
        }catch (\Exception $e){
            Log::addLogs(trans('xxm.article.handle_article').trans('fzs.common.fail'),'/article/story');
            return $this->resultJson(trans('fzs.common.fail'),0);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request)
    {
        $pager = Article::orderBy('id', 'desc')->paginate()->appends($request->all());

        return view('article.list', ['pager'=>$pager,'input'=>$request->all()]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function edit($id=0)
    {
        $article_data = ($id > 0) ? Article::article_info($id) : [];
        return view('article.edit',['id'=>$id,'article_data'=>$article_data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Article $article)
    {

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $article=new Article();
        $article->id = $id;
        $article->exists = true;
        if($article->delete()){
            Log::addLogs(trans('xxm.article.del_article').trans('fzs.common.success'),'/article/destroy/'.$id);
            return $this->resultJson('fzs.common.success',1);
        }else{
            Log::addLogs(trans('xxm.article.del_article').trans('xxm.common.fail'),'/article/destroy/'.$id);
            return $this->resultJson('fzs.common.fail',0);
        }
    }
}
